//
//  TestLabel.swift
//  ios_native_view
//
//  Created by 李旭峰 on 2020/7/10.
//
import Flutter
import SnapKit
import RxSwift
import RxCocoa

class TestLabel: NSObject {
    static let viewName:String = "plugin/test_label"
    static let viewChannelName:String = "samples.flutter.io/testLabelChannel"
    private let frame:CGRect
    private let viewID:Int64
    private let args:Any?
    var messenger:FlutterBinaryMessenger
    private var myView:UILabel = UILabel()
    private var channel:FlutterMethodChannel?
    private var isDidInit:Bool = false
    
    
    init(_ frame:CGRect,viewID:Int64,args:Any?,binaryMessenger:FlutterBinaryMessenger) {
        self.frame = frame
        self.viewID = viewID
        self.args = args
        self.messenger = binaryMessenger
        super.init()
        self.myView.isUserInteractionEnabled = true
        self.myView.frame = self.frame
        self.myView.textColor = UIColor.purple
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTapGesture))
        self.myView.addGestureRecognizer(tapGesture)
        if let params = args as? [String:Any] {
            let callBack = params["callBack"]
            print(callBack)
        }
        if let params = args as? [String:Any],let label = params["text"] as? String {
            self.myView.text = label
        }else {
            self.myView.text = "args is empty"
        }
    }
    
    private func initMathodChannel(){
        let channel = FlutterMethodChannel.init(name: "plugins.felix.angelov/textLabel_\(self.viewID)", binaryMessenger: self.messenger)
        self.channel = channel
        print(channel);
    }
    @objc private func handleTapGesture(_ gesture:UITapGestureRecognizer){
        self.channel?.invokeMethod("resetHeight", arguments: ["height":1234]);
    }
}

extension TestLabel : FlutterPlatformView {
    func view() -> UIView {
        if !self.isDidInit {
            self.isDidInit = true
            self.initMathodChannel()
//            self.initMyView()
        }
        return self.myView
    }
}
